﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using WorldMapBase;
using WorldMapNew;
using Yoozoo.Managers.ResourceManagerV2.Editor.ResourceTools;

/// <summary>
/// 打包时处理沙盘法线映射文件中预存的法线贴图 
/// </summary>
public class DisableSandBoxNormalMapBuild : IResourcePreprocessBuild, ISerializationCallbackReceiver
{
    [Tooltip("沙盘预制体根目录，遍历该目录下的所有预制体, 剔除预制体上法线贴图")]
    public void PreprocessBuildInitialize()
    {

    }

    public void SetSandBoxNormalMap()
    {
        string normalPath = "Assets/ResourcesAssets/World/mapdata/sandBoxNormalMaps.asset";
        MapNormalMap content = AssetDatabase.LoadAssetAtPath<MapNormalMap>(normalPath);
        if (content)
        {
            Dictionary<string, List<string>> obj_root = new Dictionary<string, List<string>>();
            for (int i = 0; i < content.mapNormalMap.Count; i++)
            {
                NormalData data = content.mapNormalMap[i];
                if (!obj_root.ContainsKey(data.prefabName))
                {
                    obj_root[data.prefabName] = new List<string>();
                }
                obj_root[data.prefabName].Add(data.objPath);
            }
            foreach (var assetPath in obj_root.Keys)
            {
                List<string> rootPaths = obj_root[assetPath];
                GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
                if (obj == null) continue;
                for (int ii = 0; ii < rootPaths.Count; ii++)
                {
                    string rootPath = rootPaths[ii];
                    Transform tf = obj.transform.Find(rootPath);
                    if (tf != null)
                    {
                        MeshRenderer render = tf.GetComponent<MeshRenderer>();
                        if (render != null)
                        {
                            // 填充可能存在的法线贴图
                            for (int iii = 0; iii < render.sharedMaterials.Length; iii++)
                            {
                                Material mat = render.sharedMaterials[iii];
                                if (mat && mat.HasProperty("_NormalMap"))// 判断sahder存在_NormalMap变量
                                {
                                    mat.SetTexture("_NormalMap", null);
                                }
                            }
                        }
                    }
                }
                // 查找该预制体下MeshRenderer 有没有法线贴图 有新建材质
                //MeshRenderer[] renders = obj.GetComponentsInChildren<MeshRenderer>();
                //for (int i = 0; i < renders.Length; i++)
                //{
                //    MeshRenderer render = renders[i];

                //    // 填充可能存在的法线贴图
                //    for (int ii = 0; ii < render.sharedMaterials.Length; ii++)
                //    {
                //        Material mat = render.sharedMaterials[ii];
                //        if (mat && mat.HasProperty("_NormalMap"))// 判断sahder存在_NormalMap变量
                //        {
                //            mat.SetTexture("_NormalMap", null);
                //        }
                //    }
                //}
                PrefabUtility.SavePrefabAsset(obj);
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }

    public void PreprocessBuild()
    {
        SetSandBoxNormalMap();
    }

    public void PreprocessBuildCleanup()
    {

    }

    public void OnBeforeSerialize()
    {
    
    }

    public void OnAfterDeserialize()
    {
   
    }
}